﻿Public Class frmLog

    Dim logFile As String
    Private Sub frmLog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call listInit()
        Call logListup()
    End Sub

    Private Sub listInit()
        listLogs.ShowItemToolTips = True
        listLogs.View = View.Details
        listLogs.Columns.Clear()
        listLogs.Columns.Add("로그", 150, HorizontalAlignment.Left)
        listLogs.Items.Clear()

        listDetail.ShowItemToolTips = True
        listDetail.View = View.Details
        listDetail.Columns.Clear()
        listDetail.Columns.Add("로그상세", 1500, HorizontalAlignment.Left)
        listDetail.Items.Clear()
    End Sub

    Private Sub getLog(ByVal logs As String)
        listDetail.Items.Clear()
        logFile = logs
        Try
            Dim MyStream As New System.IO.StreamReader(Application.StartupPath + "/logs/" + logs)
            Do While MyStream.Peek <> -1
                Dim LVItem As New ListViewItem
                listDetail.Items.Add(MyStream.ReadLine)
            Loop

            MyStream.Close() ' Close the StreamReader
        Catch ex As Exception
            MessageBox.Show("Error reading file." & ex.Message)
        End Try
        listDetail.EnsureVisible(listDetail.Items.Count - 1)
    End Sub

    Private Sub btnGetLog_Click(sender As System.Object, e As System.EventArgs) Handles btnGetLog.Click
        Call getLog(logFile)
    End Sub

    Private Sub logListup()
        listLogs.Items.Clear()
        Dim logDir As String = Application.StartupPath + "/logs/"
        Dim di As New IO.DirectoryInfo(logDir)
        Dim diFiles As IO.FileInfo() = di.GetFiles()

        Dim logFiles() As String = IO.Directory.GetFiles(logDir).ToArray
        Array.Reverse(logFiles)

        listLogs.Items.Clear()
        For Each file As String In logFiles
            listLogs.Items.Add(System.IO.Path.GetFileName(file))
        Next
    End Sub

    Private Sub listLogs_Click(sender As Object, e As System.EventArgs) Handles listLogs.Click
        For i As Integer = listLogs.Items.Count - 1 To 0 Step -1
            If listLogs.Items(i).Selected Then Call getLog(listLogs.SelectedItems(0).SubItems(0).Text)
        Next
    End Sub
End Class